package com.sjz.rydemo.net;

import android.text.TextUtils;


import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/8/17.
 */
public abstract class TextCallBack<T> implements Callback {

    public static final int CODE_SUCCESS = 1;
    public static final int CODE_FAILURE = 0;
    private static final String TAG = "TextCallBack";
//    Handler mHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case CODE_FAILURE:
//                    LogUtil.e("返回数据", "==========" + msg.obj);
//                    onFailure((ResponseException) msg.obj);
//                    break;
//                case CODE_SUCCESS:
//                    LogUtil.e("返回数据", "==========" + msg.obj);
//                    onSuccess((String) msg.obj);
//                    break;
//                default:
//                    break;
//            }
//        }
//    };

    /**
     * 主线程调用
     *
     * @param text
     */
    protected abstract void onSuccess(String text);

    /**
     * 主线程调用
     *
     * @param e
     */
    protected abstract void onFailure(ResponseException e);

    /**
     * 子线程调用
     *
     * @param call
     * @param e
     */
    @Override
    public void onFailure(Call call, IOException e) {
        try {
            ResponseException exception = (ResponseException) e;
//        if (e instanceof ResponseException == false) {
//            exception = new ResponseException(e);
//        } else {
//            exception = (ResponseException) e;
//        }
            onFailure(exception);
//            Message message = mHandler.obtainMessage();
//            message.what = CODE_FAILURE;
//            message.obj = exception;
//            mHandler.sendMessage(message);
        } catch (Exception ignore) {
        }

    }

    /**
     * 子线程调用
     *
     * @param call
     * @param response
     * @throws IOException
     */
    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if (response.code() == 200) {
            String text = response.body().string();
            if (!TextUtils.isEmpty(text))
                onSuccess(text);
        } else if (response.code() == 401) {
//            String text = response.body().string();
//            BaseEntity base = GsonUtils.JsonToBean(text, BaseEntity.class);
//            Toaster.show(base.getMessage());
//            LoginByWeiChatUI.startNew(XYApplication.getAppApplicationContext());
        } else if (response.code() == 403) {
//            String text = response.body().string();
//            BaseEntity base = GsonUtils.JsonToBean(text, BaseEntity.class);
//            Toaster.show(base.getMessage());
            onFailure(call, new ResponseException(response.code(), response.message()));
        } else if (response.code() == 500) {
//            String text = response.body().string();
//            BaseEntity base = GsonUtils.JsonToBean(text, BaseEntity.class);
//            Toaster.show(base.getMessage());
            onFailure(call, new ResponseException(response.code(), response.message()));
        } else {
            onFailure(call, new ResponseException(response.code(), response.message()));
        }
    }

    public static class ResponseException extends IOException {
        int code = -1;
        String msg;

        public int getCode() {
            return code;
        }


        public String getMsg() {
            return msg;
        }

        ResponseException(IOException e) {
            super(e);
        }

        ResponseException(int code, String message) {
            super(message);
            this.msg = message;
            this.code = code;
        }
    }

}
